package com.ray.controller;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.javassist.runtime.Inner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ray.bean.CompanyBean;
import com.ray.bean.CompanyMaterialTableBean;
import com.ray.bean.CustomerBean;
import com.ray.bean.DatatablesViewPageBean;
import com.ray.bean.OrderBean;
import com.ray.bean.OtherWarehouseBean;
import com.ray.bean.PurchaseOrderBean;
import com.ray.bean.ReportBean;
import com.ray.bean.UserBean;
import com.ray.bean.WarehouseInOutDetailBean;
import com.ray.exception.ServiceException;
import com.ray.mapper.ApprovalMapper;
import com.ray.mapper.BasicDataMapper;
import com.ray.mapper.CompanyMapper;
import com.ray.mapper.OtherWarehouseMapper;
import com.ray.pojo.PurchaseOrderDetailPojo;
import com.ray.service.ApprovalService;
import com.ray.service.OrderService;
import com.ray.service.WarehouseService;
import com.ray.util.ExcelExportUtil;
import com.ray.util.RequestUtil;
import com.ray.util.ResponseUtil;
import com.ray.util.ReturnStringUtil;

@SuppressWarnings("unchecked")
@Controller
@RequestMapping("order")
public class OrderController {

	@Autowired
	OrderService orderService; 
	@Autowired
	BasicDataMapper basicDataMapper;
	@Autowired
	ApprovalMapper approvalMapper;
	@Autowired
	OtherWarehouseMapper otherWarehouseMapper;
	@Autowired
	CompanyMapper companyMapper;
	@Autowired
	WarehouseService warehouseService;
	
	
	
//	获取订单总数
	@RequestMapping("/getOrderDetail")
	@ResponseBody
	public Map getOrderDetail(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(orderService.getOrderDetail(map));
	}
	
////	获取订单颜色
//	@RequestMapping("/getColor")
//	@ResponseBody
//	public Map getColor(String orderId) throws ServiceException {
//		return ResponseUtil.ResponseResult(orderService.getColor(orderId));
//	}
	
//	获取订单生产情况
	@RequestMapping("/getOrderWork")
	@ResponseBody
	public Map getOrderWork(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(orderService.getOrderWork(map));
	}
	
	
//	获取订单分颜色投料数
	@RequestMapping("/getOrderFieeding")
	@ResponseBody
	public Map getOrderDetail1(ReportBean reportBean) throws ServiceException {
		return ResponseUtil.ResponseResult(orderService.getOrderFieeding(reportBean));
	}
	
	
//	获取订单分颜色投料数
	@RequestMapping("/saveFeedOrder")
	@ResponseBody
	public Map saveFeedOrder(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		orderService.saveFeedOrder(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.SAVE_SUCCESS);
	}
	
//    获取投料单数据
	@SuppressWarnings("rawtypes")
	@RequestMapping("/getFeedOrderData")
	@ResponseBody
	public Map getFeedOrderData(HttpServletRequest request) throws ServiceException {
 		Map map=RequestUtil.getRequestAllParam(request);
 		return ResponseUtil.ResponseResult(orderService.getFeedOrderData(map));
		
	}
	
	
    //获取采购单数据，从投料单中整理出来的数据
	@RequestMapping("/getPurchaseOrderData")
	@ResponseBody
	public Map getPurchaseOrderData(HttpServletRequest request) throws ServiceException, ParseException {
		Map map=RequestUtil.getRequestAllParam(request); 
		return ResponseUtil.ResponseResult(orderService.getPurchaseOrderData(map));
		//return ResponseUtil.ResponseResult(orderService.getPurchaseOrderOther(map));
	}
	
	//保存库存采购单数据
	@RequestMapping("/getOrderInfoDetail")
	@ResponseBody
	public Map getOrderInfoDetail(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(orderService.getOrderInfoDetail(map));
	}
	
	
	//获取投料单的物料数据
	@RequestMapping("/getFeedOrderMaterialDate")
	@ResponseBody
	public DatatablesViewPageBean getFeedOrderMaterialDate(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		Object warehouse = map.get("warehouse");
		//排除原材料仓
		if(warehouse!=null && !warehouse.toString().equals("1")) {
			return orderService.getOtherWarehouseData(map); 
		}else { 
			return orderService.getFeedOrderMaterialDate(map);
		}
	}
	
	//获取订单列表
	@RequestMapping("/getOrderList")
	@ResponseBody
	public DatatablesViewPageBean getOrderList(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		return orderService.getOrderList(map);
	}
	//获取业务单订单列表
	@RequestMapping("/getOrderListForBusiness")
	@ResponseBody
	public DatatablesViewPageBean getOrderListForBusiness(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		return orderService.getOrderListForBusiness(map);
	}
	
	/**
	 * 订单查询页面
	 * @param timeType(查询的时间类型,1制单时间 2交货日期)
	 * @param startDate(开始时间)
	 * @param date (结束时间)
	 * @param deptId(部门id)
	 * @param workShop (车间id)
	 * @param type (2 未完成 1 已完成)
	 * @param dutyStatus (拖期状态  1 拖期  2 即将拖期 )
	 * @param customerId (客户型号)
	 * @param customer (客户)
	 * @param companyOrderId (公司订单编号)
	 * @param factoryId (工厂型号)
	 * @param start(分页参数)
	 * @param length (分页参数)
	 * @return
	 * @throws ParseException
	 * @throws CloneNotSupportedException 
	 */
	@RequestMapping("/getOrderMain")
	@ResponseBody
	public Map getOrderMain(HttpServletRequest request) throws ParseException, CloneNotSupportedException  {
		Map map=RequestUtil.getRequestAllParam(request);
		return orderService.getOrderMain(map);
	}

	
	/**
	 * 配料
	 * @param timeType(查询的时间类型,1制单时间 2交货日期)
	 * @param startDate(开始时间)
	 * @param date (结束时间)
	 * @param deptId(部门id)
	 * @param workShop (车间id)
	 * @param type (2 未完成 1 已完成)
	 * @param dutyStatus (拖期状态  1 拖期  2 即将拖期 )
	 * @param customerId (客户型号)
	 * @param customer (客户)
	 * @param companyOrderId (公司订单编号)
	 * @param factoryId (工厂型号)
	 * @param start(分页参数)
	 * @param length (分页参数)
	 * @return
	 * @throws ParseException
	 * @throws CloneNotSupportedException 
	 */
	@RequestMapping("/getOrderMixMaterial")
	@ResponseBody
	public Map getOrderMixMaterial(HttpServletRequest request) throws ParseException, CloneNotSupportedException  {
		Map map=RequestUtil.getRequestAllParam(request);
		return orderService.getOrderMixMaterial(map);
	}
	/**
	 * 获取未生成物料单的订单 at 2018-03-27 by 李斌
	 * @param request
	 * @param response
	 * @throws ServiceException
	 * 
	 */
//	@RequestMapping("/getFeedOrderByNotMaterial")
//	@ResponseBody
//	public DatatablesViewPageBean getFeedOrderByNotMaterial(HttpServletRequest request) throws ServiceException{
//		Map map = RequestUtil.getRequestAllParam(request);  
//		return orderService.getFeedOrderByNotMaterial(map);
//	} 
	/**
	 * 获取公司物料表信息 at 2018-03-28 by 李斌
	 * @param request
	 * @return
	 * @throws ServiceException
	 */
	@RequestMapping("/getCompanyMaterialTable")
	@ResponseBody
	public Map getCompanyMaterialTable(HttpServletRequest request) throws ServiceException{
		Map map = RequestUtil.getRequestAllParam(request);
		CompanyMaterialTableBean companyMaterialTableBean = orderService.getCompanyMaterialTable(map);
		return ResponseUtil.ResponseResult(companyMaterialTableBean);
	}
	/**
	 * 批量添加投料单主表和详情表  at 2018-03-31 by 李斌
	 * @return
	 * @throws ServiceException 
	 * @throws ParseException 
	 */
	@RequestMapping("/batchSaveFeedOrderDetailAndMain")
	@ResponseBody
	public Map batchSaveFeedOrderDetailAndMain(HttpServletRequest request) throws ServiceException, ParseException{
		Map map = RequestUtil.getRequestAllParam(request); 
		String message = orderService.saveBatchFeedOrderDetailAndMain(map,request);
		return ResponseUtil.ResponseResult(ReturnStringUtil.SAVE_SUCCESS+"  "+message);
	}
	
	/**
	 * 获取出入库的物料类型通过公司物料表 at 2018-04-02 by 李斌
	 * @return
	 * @throws ServiceException 
	 */ 
	@RequestMapping("/getMaterialTypeOnWarehouseInOutDetail")
	@ResponseBody 
	public Map getMaterialTypeOnWarehouseInOutDetail(HttpServletRequest request) throws ServiceException {
		@SuppressWarnings("rawtypes")
		Map map = RequestUtil.getRequestAllParam(request); 
		return ResponseUtil.ResponseResult(orderService.getMaterialTypeOnWarehouseInOutDetail(map));
	}
	
	/**
	 * @throws ServiceException 
	 * 获取出入库的物料名称
	* @date 2018年5月15日 上午8:49:10 
	* @author 李斌
	* @return Map 
	* @throws
	 */
	@RequestMapping("/getMaterialNameOnWarehouseInOutDetail")
	@ResponseBody
	public Map getMaterialNameOnWarehouseInOutDetail(HttpServletRequest request) throws ServiceException {
		Map map = RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(orderService.getMaterialNameOnWarehouseInOutDetail(map));
	}
	
	/**
	 * 领料单 at 2018-04-03 by 李斌
	 * @param request
	 * @return
	 * @throws ServiceException
	 */
	@RequestMapping("/saveMaterialRequisition")
	@ResponseBody
	public Map saveMaterialRequisition(HttpServletRequest request) throws ServiceException {
		@SuppressWarnings("rawtypes")
		Map map = RequestUtil.getRequestAllParam(request);
		orderService.saveMaterialRequisition(map);
		return ResponseUtil.ResponseResult("提交成功");
	}
	
	/**
	 * 获取采购单物料 通过采购单号或物料代码
	 * @param request
	 * @return
	 * @throws ServiceException
	 */
	@RequestMapping("/getPurchaseMaterial")
	@ResponseBody
	public Map getPurchaseMaterial(HttpServletRequest request) throws ServiceException {
		Map map = RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(orderService.getPurchaseMaterial(map));
	}
	
	
	/**
	 * 获取报工的部门
	 * @param request
	 * @return
	 * @throws ServiceException
	 */
	@RequestMapping("/getReportDepartment")
	@ResponseBody
	public Map getReportDepartment(HttpServletRequest request) throws ServiceException {
		Map map = RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(orderService.getPurchaseMaterial(map));
	}
	
	/**
	 * 获取报工的部门
	 * @param request
	 * @return
	 * @throws ServiceException
	 */
	@RequestMapping("/getOtherDept")
	@ResponseBody
	public Map getOtherDept(HttpServletRequest request) throws ServiceException {
		Map map = RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(orderService.getOtherDept(map));
	}
	
	
	/**
	 * 获取订单类型
	 * @param 
	 * @return
	 * @throws ServiceException
	 */
	@RequestMapping("/getOrderType")
	@ResponseBody
	public Map getOrderType(HttpServletRequest request) throws ServiceException {
		Map map = RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(orderService.getOrderType(map));
	}
	
	/**
	 * 获取报工的部门
	 * @param request
	 * @return
	 * @throws ServiceException
	 */
	@RequestMapping("/addOtherDept")
	@ResponseBody
	public Map addOtherDept(HttpServletRequest request) throws ServiceException {
		Map map = RequestUtil.getRequestAllParam(request);
		orderService.addOtherDept(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.ADD_SUCCESS);
	}
	
	
	/**
	 * 设置订单车间生产顺序
	 * @param orderId
	 * @param workShopId
	 * @param time
	 * @return
	 * @throws ServiceException
	 */
	@RequestMapping("/setOrderWorkShopTime")
	@ResponseBody
	public Map setOrderWorkShopTime(HttpServletRequest request) throws ServiceException {
		Map map = RequestUtil.getRequestAllParam(request);
		orderService.setOrderWorkShopTime(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.SET_SUCCESS);
	}
	
	/**
	 * 修改订单下单日期
	 * @param orderId
	 * @param time
	 * @return
	 * @throws ServiceException
	 */
	@RequestMapping("/updateOrderXianDanDate")
	@ResponseBody
	public Map updateOrderXianDanDate(HttpServletRequest request) throws ServiceException {
		Map map = RequestUtil.getRequestAllParam(request);
		orderService.updateOrderXianDanDate(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.UPDATE_SUCCESS);
	}
	
	
	/**
	 * 获取订单物料报工情况
	 * @param orderId(订单编号)
	 * @return
	 * @throws ServiceException
	 */
	@RequestMapping("/getOrderMaterialReport")
	@ResponseBody
	public Map getOrderMaterialReport(HttpServletRequest request) throws ServiceException {
		Map map = RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(orderService.getOrderMaterialReport(map));
	}
	
	
	
	
	/**
	 * 修改采购单明细数据,如复期,采购日期,是否打印等..
	* @date 2018年5月11日 下午2:11:15 
	* @author 李斌
	* @return Map<String,Object> 
	* @throws
	 */
	@RequestMapping("/updatePurchaseOrderDetail")
	@ResponseBody 
	public Map<String,Object> updatePurchaseOrderDetail(HttpServletRequest request) throws ServiceException{
		Map<String,Object> map = RequestUtil.getRequestParamByGeneric(request);
		orderService.updatePurchaseOrderDetail(map);
		return ResponseUtil.ResponseResult("修改成功");
	}
	
	
	@RequestMapping("/updateBatchPurchaseOrderDetail")
	@ResponseBody
	public Map<String,Object> updateBatchPurchaseOrderDetail(HttpServletRequest request) throws ServiceException{
		Map<String,Object> map = RequestUtil.getRequestParamByGeneric(request);
		orderService.updateBatchPurchaseOrderDetail(map);
		return ResponseUtil.ResponseResult("修改成功");
	} 
	
	/**
	 * 获取订单审核图片
	 * @param request
	 * @param orderId(订单编号)
	 * @return
	 * @throws ServiceException
	 */
	@RequestMapping("/getOrderApprovalImg")
	@ResponseBody
	public Map<String,Object> getOrderApprovalImg(HttpServletRequest request) throws ServiceException{
		Map<String,Object> map = RequestUtil.getRequestParamByGeneric(request);
		return ResponseUtil.ResponseResult(orderService.getOrderApprovalImg(map));
	} 
	
	/**
	 * 查询供应商名称从采购单主表中获取
	* @date 2018年5月16日 上午8:56:52 
	* @author 李斌
	* @return Map<String,Object> 
	* @throws
	 */
	@RequestMapping("/getProviderNameByPurchaseOrder")
	@ResponseBody
	public Map<String,Object> getProviderNameByPurchaseOrder(HttpServletRequest request) throws ServiceException{
		Map<String,Object> map = RequestUtil.getRequestParamByGeneric(request);
		return ResponseUtil.ResponseResult(orderService.getProviderNameByPurchaseOrder(map));
	}
	
	/**
	 * 修改投料单物料信息
	* @date 2018年6月11日 下午7:53:12 
	* @author 李斌
	* @return Map<String,Obejct> 
	* @throws
	 */
	@RequestMapping("/updateFeedDetailMaterial")
	@ResponseBody
	public Map<String,Object> updateFeedDetailMaterial(HttpServletRequest request){
		Map<String,Object> map = RequestUtil.getRequestParamByGeneric(request);
		int row = orderService.updateFeedDetailMaterial(map);
		String messsage = "";
		if(row == 1) {
			messsage = "该投料单有占用块料数,因物料修改,块料占用数被清除";
		}
		return ResponseUtil.ResponseResult("更新成功"+messsage);
	}  
	
	/**
	 * 通过投料单明细ids获取投料单信息
	* @date 2018年8月4日 上午7:57:01 
	* @author 李斌
	* @return Map<String,Object> 
	* @throws
	 */
	@RequestMapping("/getFeedOrderDetailByDetailIds")
	@ResponseBody
	public Map<String,Object> getFeedOrderDetailByDetailIds(HttpServletRequest request){
		Map<String,Object> map = RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(orderService.getFeedOrderDetailByDetailIds(map));
	}
	
	/**
	 * 获取成品仓的订单编号
	* @date 2018年8月17日 下午1:21:46 
	* @author 李斌
	* @return Map<String,Object> 
	* @throws
	 */
	@RequestMapping("/getOrderIdListByOtherWarehouse")
	@ResponseBody
	public Map<String,Object> getOrderIdListByOtherWarehouse(HttpServletRequest request){
		Map<String,Object> map = RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(otherWarehouseMapper.getOrderIdListByOtherWarehouse(map));
	}
	
	
	/**
	 * 获取公司订单
	 * @param request
	 * nete 
	 * @return
	 */
	@RequestMapping("/getCompanyOrder")
	@ResponseBody
	public Map<String,Object> getCompanyOrder(HttpServletRequest request){
		Map map = RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(orderService.getCompanyOrder(map));
	}
	
	/**
	 * 订单查询页面查询明细
	 * @param timeType(查询的时间类型,1制单时间 2交货日期)
	 * @param startDate(开始时间)
	 * @param date (结束时间)
	 * @param deptId(部门id)
	 * @param workShop (车间id)
	 * @param type (2 未完成 1 已完成)
	 * @param dutyStatus (拖期状态  1 拖期  2 即将拖期 )
	 * @param customerId (客户型号)
	 * @param customer (客户)
	 * @param companyOrderId (公司订单编号)
	 * @param factoryId (工厂型号)
	 * @param start(分页参数)
	 * @param length (分页参数)
	 * @param request
	 * @return
	 * @throws ParseException 
	 * @throws CloneNotSupportedException 
	 */
	@RequestMapping("/queryOrderPageDateil")
	@ResponseBody
	public Map<String,Object> queryOrderPageDateil(HttpServletRequest request) throws ParseException, CloneNotSupportedException{
		Map map = RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(orderService.queryOrderPageDateil(map));
	}
	
	@RequestMapping("/getWarehouseInOutDetailByIds")
	@ResponseBody
	public DatatablesViewPageBean<WarehouseInOutDetailBean> getWarehouseInOutDetailByIds(HttpServletRequest request){
		Map<String,Object> map = RequestUtil.getRequestAllParam(request);
		return warehouseService.getWarehouseInOutDetailByIds(map);
	} 
	
	
	//下载员工达成率数据
	@RequestMapping(value="/downlodeOrderWhere",method=RequestMethod.GET)
	@ResponseBody
	public void downlodeReachFile(HttpServletRequest request,HttpServletResponse response) throws ServiceException, CloneNotSupportedException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, IOException, ParseException{
		Map map=RequestUtil.getRequestAllParam(request);
		map.put("start", null);
		map.put("length", null);
		//固定表头
		String [] tableHeadFields={"订单编号","工厂型号","客户型号","客户订单号","客户","尺寸","下单日期","交货日期","订单总数","采购","入库","外协"};
		String [] changeHead= {"完成情况","人工排期","物料","完成日期","预计完成日期","数量"};
//	    List<String> strList=new ArrayList(Arrays.asList(tableHeadFields));
		Map resultMap=orderService.getOrderMain(map);
		String [] fields={"companyOrderId","factoryId","customerId","customerOrderId","customer","chiCun","xiaDanDate","jiaoDate","orderCount","purchaseStatus","inStatus","putOutStatus"};
		String tempFolder=request.getServletContext().getRealPath("/")+"tempExcelFolder";
		File excelFile=orderService.createExcelFile(resultMap,tempFolder,tableHeadFields,changeHead,fields);
		ResponseUtil.downloadfile(response, excelFile, "订单查询.xlsx", true);
	}
	
    
}
